﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;
using BusinessFacade;
using Entities;
using System.Security.Cryptography;

namespace MedicareNoteManagerWeb.Web
{
    public partial class Users_add :BasePage
    {
        sys_user_Model model1 = new sys_user_Model();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDrop();
            }
        }
        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.DropArea);
        }
        public string CheckData()
        {
            string strErr = "";
            if (string.IsNullOrEmpty(txt_UserName.Text.Trim()))
            {
                strErr += "用户名不能为空";
            }
            if (string.IsNullOrEmpty(txt_Password.Text.Trim()))
            {
                strErr += "密码不能为空";
            }
            if (DropArea.SelectedValue == "0")
            {
                strErr += "请选择区域";
            }
            return strErr;
        }
            protected void Button4_Click(object sender, EventArgs e)
            {
                model1 = (sys_user_Model)Session["UserModel"];
                string strErr = CheckData();
                if (string.IsNullOrEmpty(strErr))
                {
                    sys_user_Model model = new sys_user_Model();
                    model.AreaID = int.Parse(DropArea.SelectedValue);
                    model.LoginName = txt_UserName.Text;
                    //model.LoginPassword = txt_Password.Text;
                    string pwd = txt_Password.Text;
                    ////获取加密服务   
                    //MD5CryptoServiceProvider md5CSP = new MD5CryptoServiceProvider();   
                    ////获取要加密的字段，并转化为Byte[]数组   
                    //byte[] testEncrypt = System.Text.Encoding.Unicode.GetBytes(pwd);   
                    ////加密Byte[]数组   
                    //byte[] resultEncrypt = md5CSP.ComputeHash(testEncrypt); 
                    ////将加密后的数组转化为字段(普通加密)   
                    //string testResult = System.Text.Encoding.Unicode.GetString(resultEncrypt);   

                    //作为密码方式加密    
                    string Encrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5");
                    model.LoginPassword = Encrypt;
                    model.CreateDate = DateTime.Now;
                    //model.CreateUserID = model1.UserID;
                    model.UserID = 0;
                    if (new Busi_sys_user().Add(model))
                    {
                        MessageBox.ShowDialog(Page, "添加成功！", "success");
                    }
                    else
                    {
                        MessageBox.ShowDialog(Page, "添加失败！", "error");
                    }
                }
                else
                {
                    MessageBox.ShowDialog(Page, "添加失败！" + strErr, "error");
                }
            }
    }
}